package com.jiezuo.flowable.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;


import com.jiezuo.common.base.BaseController;
import com.jiezuo.common.enums.ResultEnums;
import com.jiezuo.common.util.PageUtil;
import com.jiezuo.common.util.ResultUtil;
import com.jiezuo.common.vo.PageVo;
import com.jiezuo.common.vo.Result;
import com.jiezuo.flowable.entity.MyFormDesign;
import com.jiezuo.flowable.service.MyFormDesignService;

import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.form.FormProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* @description   表单设计器
* @author        庞留杰
* @date          2024/03/06 16:49
* @version       V1.0
**/
@Slf4j
@Transactional
@RequestMapping("/myFormDesign")
@RestController
//表单设计器", tags = {"表单设计器接口"})
public class MyFormDesignController  extends BaseController{

    @Autowired
    private MyFormDesignService service;

    //查询带分页列表", notes = "查询带分页列表")
    @GetMapping("/findPage")
    public Result<IPage<MyFormDesign>> findPage(@ModelAttribute PageVo pageVo,@ModelAttribute MyFormDesign myFormDesign) {
        IPage<MyFormDesign> page = service.findPage(PageUtil.initMpPage(pageVo),myFormDesign);
        return new ResultUtil<IPage<MyFormDesign>>().setData(page);
    }

    //查询不带分页列表", notes = "查询不带分页列表")
    @GetMapping("/findList")
    public Result<List<MyFormDesign>> findList(@ModelAttribute MyFormDesign myFormDesign) {
        List<MyFormDesign> list = service.findList(myFormDesign);
        return new ResultUtil<List<MyFormDesign>>().setData(list);
    }

    //通过ID查询", notes = "通过ID查询")
    @GetMapping("/findById")
    public Result<MyFormDesign> findById(String id) {
        MyFormDesign myFormDesign = service.findById(id);
        return new ResultUtil<MyFormDesign>().setData(myFormDesign);
    }

    //添加", notes = "添加")
    @PostMapping("/save")
    public Result save(@RequestBody MyFormDesign myFormDesign) {
        boolean boo = service.add(myFormDesign);
        if(boo){
            return new ResultUtil().setSuccessMsg(ResultEnums.OK.getReasonPhraseCN());
        }else {
            return new ResultUtil().setErrorMsg(ResultEnums.FAIL.getReasonPhraseCN());
        }
    }

    //修改", notes = "修改")
    @PostMapping("/update")
    public Result update(@RequestBody MyFormDesign myFormDesign) {
        boolean boo = service.update(myFormDesign);
        if(boo){
            return new ResultUtil().setSuccessMsg(ResultEnums.OK.getReasonPhraseCN());
        }else {
            return new ResultUtil().setErrorMsg(ResultEnums.FAIL.getReasonPhraseCN());
        }
    }

    //删除", notes = "删除【逻辑删除】")
    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable String id) {
        boolean boo = service.delete(id);
        if(boo){
            return new ResultUtil().setSuccessMsg(ResultEnums.OK.getReasonPhraseCN());
        }else {
            return new ResultUtil().setErrorMsg(ResultEnums.FAIL.getReasonPhraseCN());
        }
    }

    //通过ID，获取表单属性", notes = "通过ID，获取表单属性")
    @GetMapping("/findPropertiesById")
    public Result<JSONArray> findPropertiesById(String id) {
        JSONArray properties = service.findPropertiesById(id);
        return new ResultUtil<JSONArray>().setData(properties);
    }
    // 用户发起流程时，获取需要填写表单的数据
    @GetMapping("/findFormByKey")
    public Result<JSONObject> findFormByKey(String processDefinitionKey,@RequestParam(required = false) String businessId) {
        JSONObject properties = service.findFormByKey(processDefinitionKey,businessId);
        return new ResultUtil<JSONObject>().setData(properties);
    }

    // 用户审核时，获取需要填写表单的数据
    @GetMapping("/findFormByTaskId")
    public Result<JSONObject> findFormByTaskId(String taskId) {
        JSONObject properties = service.findFormByTaskId(taskId);
        return new ResultUtil<JSONObject>().setData(properties);
    }
    // 审批过的，查询详情
    @GetMapping("/findFormByProcessInstanceId")
    public Result<JSONObject> findFormByProcessInstanceId(String processInstanceId) {
        JSONObject properties = service.findFormByProcessInstanceId(processInstanceId);
        return new ResultUtil<JSONObject>().setData(properties);
    }
    @GetMapping("/findFormByBusinessId")
    public Result<JSONObject> findFormByBusinessId(@RequestParam String code,@RequestParam String business_id) {
        JSONObject properties = service.findFormByBusinessId(code,business_id);
        return new ResultUtil<JSONObject>().setData(properties);
    }
    // 通过流程id查询表单是否存在
    @GetMapping("/findFormIsExistByProcessInstanceId")
    public Result<Boolean> findFormIsExistByProcessInstanceId(String processInstanceId) {
        Boolean isExist = service.findFormIsExistByProcessInstanceId(processInstanceId);
        return new ResultUtil<Boolean>().setData(isExist);
    }
    // 通过流程id查询表单是否存在
    @GetMapping("/findFormIsExistByBusinessId")
    public Result<Boolean> findFormIsExistByBusinessId(@RequestParam String code,@RequestParam String business_id) {
        Boolean isExist = service.findFormIsExistByBusinessId(code,business_id);
        return new ResultUtil<Boolean>().setData(isExist);
    }

    // 获取表单属性
    @GetMapping("/findFormFieldByKey")
    public Result<List<FormProperty>> findFormFieldByKey(String processDefinitionKey,@RequestParam(required = false) String taskId) {
        List<FormProperty> properties = service.findFormFieldByKey(processDefinitionKey,taskId);
        return new ResultUtil<List<FormProperty>>().setData(properties);
    }

}
